/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ebook;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
/**
 *
 * @author TOSHIBA
 */
public class HandleCon {
private static String		mysqlDbURL		= "jdbc:mysql://127.0.0.1:3306/kutuphane";
	private static Connection	conn			= null;
	private static String USERNAME = "root";
	private static String PASSWORD = "";
	private static final String	mysqlDriverName	= "com.mysql.jdbc.Driver";
 public static Connection getConnInstance() {

		try{
			if (conn == null) {
				conn = createConnection(USERNAME, PASSWORD);
			}
			if (conn.isClosed()){
				conn = createConnection(USERNAME,PASSWORD);
			}
		}
		catch(Exception e)
		{
			conn = createConnection(USERNAME,PASSWORD);
		}

		return conn;
	}

	/**
	 * It provides a secure connection
	 * @param userName
	 * @param password
	 * @return
	 */
//
	private static Connection createConnection(String userName, String password) {

		try {
			Class.forName(mysqlDriverName).newInstance();
			Properties dbProps = new Properties();
			dbProps.put("user", userName);
			dbProps.put("password", password);

			conn = DriverManager.getConnection(mysqlDbURL, dbProps);
			System.out.println("Connection successful!");
		} catch (Exception except) {
			System.out.print("Could not connect to the database with username:" + userName);
			System.out.println("password" + password);
			except.printStackTrace();
		}
		return conn;
	}
////
}
